﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Customer_Profile.Admin
{
    public partial class UserGroupManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dtgDataBind();
        }

        private void dtgDataBind()
        {
            DataSet ds = GroupDao.GetAllGroup();
            if (ds != null)
            {
                dtgUserGroup.DataSource = ds; //ds_1.Tables[0].DefaultView;
                dtgUserGroup.DataBind();
            }
        }


        protected void dtgUserGroup_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string id = e.Item.Cells[0].Text;
            if (e.CommandName == "cmdMenuAuth")
            {
                Response.Redirect("MenuAuthority.aspx?id=" + id, true);
            }
            if (e.CommandName == "cmdDataAuth")
            {
                Response.Redirect("DataAuthority.aspx?id=" + id, true);
            }
            if (e.CommandName == "cmdUser")
            {
                Response.Redirect("GroupUser.aspx?id=" + id, true);
            }
            else if (e.CommandName == "cmdModify")
            {
                Response.Redirect("UsergroupModify.aspx?id=" + id, true);
            }
            else if (e.CommandName == "cmdDel")
            {
                try
                {
                    if (GroupDao.IsExistGrpMenu(id))
                    {
                        KVB.Common.JavaScript.ViewAlertMsg("This Group have Permissions,Can't Delete!");
                        return;
                    }
                    else
                    {
                        GroupDao.DeleteGroup(id);

                        //记录用户操作日志
                        //CVOC.Comm.WriteSystemLog(Session["SS_USER_ID"].ToString(), Utility.ReplaceChar(Utility.ReplaceQuote(Request["menu_idx"].ToString()), true), "04", Page.Request.UserHostAddress, Page.Request.Path, "删除ID为“" + id + "”用户组，操作成功");

                    }
                }
                catch
                {
                }
                dtgDataBind();
            }
        }

        protected void dtgUserGroup_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                LinkButton Delbtn = (LinkButton)e.Item.Cells[7].Controls[0];
                Delbtn.Attributes.Add("onclick", "javascript:return confirm('Are you Sure?');");
            }
        }

        protected void Add_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("UserGroupAdd.aspx", true);
        }

        protected void dtgUserGroup_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {            
            this.dtgUserGroup.CurrentPageIndex = e.NewPageIndex;
            this.dtgUserGroup.DataBind();
        }
    }
}